package test.bean;

import java.math.BigInteger;
import java.rmi.RemoteException;
import java.util.Properties;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

public class ClientEJB {

	public static void main(String[] args) {
		Context jndiContext;
		UserTransaction ut = null;
		try {
			jndiContext = getInitialContext();
			Object refEntity = jndiContext
					.lookup("java:comp/env/ejb/test/TestEntityHomeRemote");
			TestEntityHomeRemote entityHomeRemote = (TestEntityHomeRemote) PortableRemoteObject
					.narrow(refEntity, TestEntityHomeRemote.class);

			ut = (UserTransaction) jndiContext
					.lookup("javax.transaction.UserTransaction");
			
			TestEntityRemote entity = entityHomeRemote.create(9000,
					"FIX IT");
			entity.stageLog("END BASE ENTITY CREATE");
			
			entity.stageLog("START findByPrimaryKey");
			TestEntityRemote found=entityHomeRemote.findByPrimaryKey(BigInteger.valueOf(9000));
			entity.stageLog("END findByPrimaryKey");
			
			entity.stageLog("START Entity Business");
			System.out.println(found.getEcho());
			entity.stageLog("END Entity Business");
			
			found.setEcho("123");
			
			entity.stageLog("START Entity Business IN TRANSACTION");
			ut.begin();
			found.setEcho("321");
			ut.commit();
			entity.stageLog("END Entity Business IN TRANSACTION");
			
			entity.stageLog("\n\nSTART create 5_entity over poll");
			entityHomeRemote.create(Integer.valueOf(1),
					"0");
			entityHomeRemote.create(Integer.valueOf(2),
					"1");
			entityHomeRemote.create(Integer.valueOf(3),
					"10");
			entityHomeRemote.create(Integer.valueOf(4),
					"11");
			entityHomeRemote.create(Integer.valueOf(5),
					"100");
			entity.stageLog("END create 5_entity over poll\n\n");
			
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Object refSession=jndiContext
					.lookup("java:comp/env/ejb/test/TestSessionFulHomeRemote");
			TestSessionHomeRemote sessionHomeRemote = (TestSessionHomeRemote) PortableRemoteObject
					.narrow(refSession, TestSessionHomeRemote.class);
			
			entity.stageLog("START Session stateful create");
			sessionHomeRemote.create();
			entity.stageLog("END Session stateful create");
			
			entity.stageLog("START Session stateful create");
			ut.begin();
			TestSessionRemote session=sessionHomeRemote.create();
			ut.commit();
			entity.stageLog("END Session stateful create");
			
			entity.stageLog("START Session stateful business");
			System.out.println(session.myLog());
			entity.stageLog("END Session stateful business");
			
			entity.stageLog("START Session stateful business in TRANSACTION");
			System.out.println(session.myTransaction());
			entity.stageLog("END Session stateful business in TRANSACTION");

			entity.stageLog("START Session stateful remove");
			session.remove();
			entity.stageLog("END Session stateful remove");
			
			entity.stageLog("START Entity findByPrimaryKey & remove");
			entityHomeRemote.remove(entityHomeRemote.findByPrimaryKey(BigInteger.valueOf(5)));
			entity.stageLog("END Entity findByPrimaryKey & remove");

			entity.stageLog("START Entity findBy<Suffix>");
			System.out.println(entityHomeRemote.findByEcho("321"));
			entity.stageLog("END Entity findBy<Suffix>");
			
			entity.stageLog("START Entity findByPrimaryKey");
			TestEntityRemote entity1=entityHomeRemote.findByPrimaryKey(BigInteger.valueOf(3));
			entity.stageLog("END Entity findByPrimaryKey");
			
			entity.stageLog("START Entity business");
			System.out.println(entity1.fact());
			entity.stageLog("END Entity business");
			
			entity.stageLog("START Entity remove");
			found.remove();
			entity1.remove();
			entity.stageLog("START BASE Entity remove");
			entity.remove();
			entity.stageLog("END BASE Entity remove");
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CreateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FinderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoveException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotSupportedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SystemException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (RollbackException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (HeuristicMixedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (HeuristicRollbackException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
//		}catch(Throwable t){
//			try {
//				if(ut!=null&&ut.getStatus()!=Status.STATUS_NO_TRANSACTION)
//				ut.rollback();
//			} catch (IllegalStateException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (SecurityException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (SystemException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}

	}

	public static Context getInitialContext() throws NamingException {
		Properties properties = new Properties();
		properties.put(Context.INITIAL_CONTEXT_FACTORY,
				"weblogic.jndi.WLInitialContextFactory");
		properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
		properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
		properties.put(Context.SECURITY_CREDENTIALS, "blackberry9800");
		return new InitialContext(properties);
	}

//	public static void myTransaction() {
//
//	}

}
